package com.wxprogram.smallprogram.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.wxprogram.smallprogram.entity.Major;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.wxprogram.smallprogram.dao.CityDao;
import com.wxprogram.smallprogram.dao.MajorDao;
import com.wxprogram.smallprogram.dao.SchoolDao;
import com.wxprogram.smallprogram.entity.City;
import com.wxprogram.smallprogram.entity.MajorType;
import com.wxprogram.smallprogram.entity.School;
import com.wxprogram.smallprogram.service.ViewInfoService;

import javax.annotation.Resource;

@Service
public class ViewInfoServiceImpl implements ViewInfoService {
	@Autowired
	private CityDao cityDao;
	@Autowired
	private SchoolDao schoolDao;
	@Autowired
	private MajorDao majorDao;
	@Resource
	private RedisTemplate<String,Object> redisTemplate;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public List<City> getCity() {
		City city = null;
		School school = null;
		String keyBase = "city";
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(keyBase);
		List<City> cities =new ArrayList<>();
		if(hasKey){
			cities = (List<City>) operations.get(keyBase);
			logger.info("从缓存获得数据 key:"+keyBase);
		}else {
			cities = cityDao.selectCityAll();
			operations.set(keyBase,cities);
			logger.info("从数据库查询数据并存入缓存key:"+keyBase);
		}

		/**
		 * 获取第一个城市所有学校及第一个学校所有专业类型
		 */
		if(cities != null && cities.size() > 0){
			city = cities.get(0);
			String keyC = "cityname_"+city.getCityName();
			boolean hasKey2 = redisTemplate.hasKey(keyC);
			List<School> schools=new ArrayList<>();
			if(hasKey2){
				schools= (List<School>) operations.get(keyC);
				logger.info("从缓存获得数据 key: " + keyC);
			}else {
				schools = schoolDao.SelectSchoolByCity(city.getCityName());
				operations.set(keyC,schools);
				logger.info("从数据库查询数据并存入缓存key:"+keyC);
			}
			city.setSchools(schools);
		}
		if(city.getSchools() != null && city.getSchools().size()>0){
			school = city.getSchools().get(0);
			String keyX="schoolname_"+school.getSchoolname();
			boolean hasKey1 = redisTemplate.hasKey(keyX);
			List<MajorType> majorTypes =new ArrayList<>();
			if(hasKey1){
				majorTypes = (List<MajorType>) operations.get(keyX);
				logger.info("从缓存获得数据key: "+keyX);
			}else{
				majorTypes = majorDao.selectMajorTypeBySchool(school.getSchoolname());
				operations.set(keyX,majorTypes);
				logger.info("从数据库查询数据并存入缓存key"+keyX);
			}
			school.setMajorTypes(majorTypes);
		}
		return cities;
	}

	public List<School> getSchoolAndType(String cityName) {
		School school = null;
		String keyC = "cityname_"+cityName;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(keyC);
		List<School> schools=new ArrayList<>();
		if(hasKey){
			schools= (List<School>) operations.get(keyC);
			logger.info("从缓存获得数据 key:"+keyC);
		}else {
			schools = schoolDao.SelectSchoolByCity(cityName);
			operations.set(keyC,schools);
			logger.info("从数据库查询数据并存入缓存key:"+keyC);
		}
		/**
		 * 获取第一个学校专业
		 */
		if (schools != null && schools.size() > 0) {
			school = schools.get(0);
			school.setMajorTypes(majorDao.selectMajorTypeBySchool(school.getSchoolname()));
		}
		return schools;
	}

	public List<School> geMajorTypes(String schoolName,String cityname) {
		String keyC = "cityname_"+cityname;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(keyC);
		List<School> schools=new ArrayList<>();
		if(hasKey){
			schools= (List<School>) operations.get(keyC);
			logger.info("从缓存获得数据key: "+keyC);
		}else {
			schools = schoolDao.SelectSchoolByCity(cityname);
			operations.set(keyC,schools);
			logger.info("从数据库查询数据并存入缓存key"+keyC);
		}
		String keyX="schoolname_"+schoolName;
		boolean hasKey1 = redisTemplate.hasKey(keyX);
		List<MajorType> majorTypes =new ArrayList<>();
		if(hasKey1){
			majorTypes = (List<MajorType>) operations.get(keyX);
			logger.info("从缓存获得数据key: "+keyX);
		}else{
			majorTypes = majorDao.selectMajorTypeBySchool(schoolName);
			operations.set(keyX,majorTypes);
			logger.info("从数据库查询数据并存入缓存key"+keyX);
		}
		for (School school:schools) {
			if (schoolName.equals(school.getSchoolname())){
				school.setMajorTypes(majorTypes);
			}
		}
		return schools;
	}

	//获取城市，学校，或者专业类型
	@Override
	public List getChoice(String cityname,String schoolName){
		if (cityname != null && schoolName == null){
			return getSchoolAndType(cityname);
		}else if (schoolName != null && cityname != null){
			return geMajorTypes(schoolName,cityname);
		}else {
			return getCity();
		}
	}

	/**
	 * 获取城市，学校，或者专业类型或者专业
	 * @param city
	 * @param school
	 * @param majortyep
	 * @return
	 */
	@Override
	public List getItem(String city,String school,String majortyep){

		if (city != null && school == null && majortyep == null){
			School school2 = null;
			MajorType majortype2 = null;
			List<School> schools = schoolDao.SelectSchoolByCity(city);
			if (schools != null && schools.size() > 0) {
				school2 = schools.get(0);
				school2.setMajorTypes(majorDao.selectMajorTypeBySchool(school2.getSchoolname()));

				if (school2.getMajorTypes() != null && school2.getMajorTypes().size() > 0){
					majortype2 = school2.getMajorTypes().get(0);
					majortype2.setMajors(majorDao.selectMajor(majortype2.getMajortype(),school2.getSchoolname()));
				}

			}
			return schools;
		}else if(city != null && school != null && majortyep ==null){
			MajorType majortype2 = null;
			List<MajorType> majorTypes = majorDao.selectMajorTypeBySchool(school);
			if (majorTypes != null && majorTypes.size()> 0){
				majortype2 = majorTypes.get(0);
				majortype2.setMajors(majorDao.selectMajor(majortype2.getMajortype(),school));
			}
			return  majorTypes;
		}else if (city != null && school != null && majortyep !=null){

			List<Major> majors = majorDao.selectMajor(majortyep,school);

			return majors;

		}else{
			City city2 = null;
			School school2 = null;
			MajorType majortype2 = null;
			List<City> cities = cityDao.selectCityAll();
			if(cities != null && cities.size() > 0){
				city2 = cities.get(0);
				city2.setSchools(schoolDao.SelectSchoolByCity(city2.getCityName()));

				if(city2.getSchools() != null && city2.getSchools().size()>0){
					school2 = city2.getSchools().get(0);
					school2.setMajorTypes(majorDao.selectMajorTypeBySchool(school2.getSchoolname()));

					if (school2.getMajorTypes() != null && school2.getMajorTypes().size() > 0){
						majortype2 = school2.getMajorTypes().get(0);
						majortype2.setMajors(majorDao.selectMajor(majortype2.getMajortype(),school2.getSchoolname()));
					}
				}
			}

			return cities;

		}
	}


}
